package com.wang.users.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.wang.common.pojo.ApiResponse;
import com.wang.common.pojo.ApiResponseEnum;
import com.wang.common.pojo.JSONEnum;
import com.wang.common.service.impl.BaseServiceImpl;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IAAdminsDao;
import com.wang.users.dao.IAUsersDao;
import com.wang.users.dao.impl.AAdminsDaoImpl;
import com.wang.users.dao.impl.AUsersDaoImpl;
import com.wang.users.pojo.AAdmins;
import com.wang.users.pojo.AAdminsEnum;
import com.wang.users.pojo.AUsers;
import com.wang.users.pojo.AUsersEnum;
import com.wang.users.service.IUsersDbService;

/**
 * usersService的实现类 Service需要通过读写器赋值
 *
 * @author wangshMac
 */
public class UsersDbServiceImpl extends BaseServiceImpl implements IUsersDbService {
    private IAAdminsDao adminsDao = new AAdminsDaoImpl();
    private IAUsersDao usersDao = new AUsersDaoImpl();

    @Override
    public ApiResponse<Object> saveOneAdminsService(AAdmins admins) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        admins.setStatus(AAdminsEnum.STATUS_ENABLE.getStatus());
        admins.setCreateTime(new Date());
        admins.setUpdateTime(new Date());
        admins.setLastLoginTime(new Date());
        int res = this.adminsDao.saveOne(admins);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), admins.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), admins);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneAdminsService(AAdmins admins) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        admins.setUpdateTime(new Date());
        int res = this.adminsDao.updateOne(admins);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), admins.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), admins);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneAdminsService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.adminsDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AAdmins> apiResponse = new ApiResponse<AAdmins>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AAdmins> demoAdminsList = Collections.EMPTY_LIST;

        Map<String, List<AAdmins>> dataList = new HashMap<String, List<AAdmins>>();
        demoAdminsList = this.adminsDao.findCondList(pageInfoUtil, condMap);
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoAdminsList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoAdminsList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AAdmins> findOneAdminsService(Map<String, Object> condMap) {
        ApiResponse<AAdmins> apiResponse = new ApiResponse<AAdmins>();

        Map<String, AAdmins> data = new HashMap<String, AAdmins>();
        AAdmins admins = this.adminsDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), admins);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(admins);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> saveOneUsersService(AUsers users) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        /* 查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.clear();
        condMap.put("email", users.getEmail());
        ApiResponse<AUsers> apiRes = this.findOneUsersService(condMap);
        /* 获取对象 */
        AUsers userRes = apiRes.getDataOneJava();
        if (userRes != null) {
            /* 用户名已经存在,请换一个 */
            apiResponse.setCode(ApiResponseEnum.INFO_USERNAME_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }

        users.setCreateTime(new Date());
        users.setUpdateTime(new Date());
        users.setLastLoginTime(new Date());
        int res = this.usersDao.saveOne(users);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), users.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), users);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneUsersService(AUsers users) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        /* 查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.clear();
        condMap.put("email", users.getEmail());
        ApiResponse<AUsers> apiRes = this.findOneUsersService(condMap);
        /* 获取对象 */
        AUsers userRes = apiRes.getDataOneJava();
        if (userRes != null && userRes.getId() != users.getId()) {
            /* 用户名已经存在,请换一个 */
            apiResponse.setCode(ApiResponseEnum.INFO_USERNAME_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }

        users.setUpdateTime(new Date());
        int res = this.usersDao.updateOne(users);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), users.getId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), users);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneUsersService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.usersDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AUsers> findCondListUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<AUsers> apiResponse = new ApiResponse<AUsers>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<AUsers> demoUsersList = Collections.EMPTY_LIST;

        Map<String, List<AUsers>> dataList = new HashMap<String, List<AUsers>>();
        demoUsersList = this.usersDao.findCondList(pageInfoUtil, condMap);
        /* 查询扩展信息 */
        Map<String,Object> paramsCondMap = new HashMap<>();
        for (AUsers dataTemp : demoUsersList) {
            paramsCondMap.put("id", dataTemp.getTeacherId());
            AUsers dbOneRes = this.usersDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                dataTemp.setTeacher(dbOneRes);
            }
        }
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoUsersList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoUsersList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<AUsers> findOneUsersService(Map<String, Object> condMap) {
        ApiResponse<AUsers> apiResponse = new ApiResponse<AUsers>();

        Map<String, AUsers> data = new HashMap<String, AUsers>();
        AUsers users = this.usersDao.findOne(condMap);
        if (Objects.nonNull(users)) {
            /* 查询扩展信息 */
            Map<String,Object> paramsCondMap = new HashMap<>();
            paramsCondMap.put("id", users.getTeacherId());
            AUsers dbOneRes = this.usersDao.findOne(paramsCondMap);
            if (Objects.nonNull(dbOneRes)) {
                users.setTeacher(dbOneRes);
            }
        }
        data.put(ApiResponseEnum.NAME_ONE.getName(), users);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(users);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}
